package cn.itzzx.lagou.controller;

import cn.itzzx.lagou.datasource.JdbcTemplateFactory;
import org.apache.curator.framework.CuratorFramework;
import org.apache.zookeeper.CreateMode;
import org.apache.zookeeper.data.Stat;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;

/**
 * TODO
 *
 * @author Martin
 * @since 1.0
 */
@RestController
public class TestController {

    @Resource
    private JdbcTemplate jdbcTemplateFactory;

    @Resource
    private CuratorFramework curatorFramework;

    @Value("${dataSourcePath}")
    private String dataSourcePath;

    @GetMapping("getNow")
    public String get() {
        String now = jdbcTemplateFactory.queryForObject("select now() as now", String.class);
        return now;
    }


    @GetMapping("set")
    public String setDatasource() throws Exception {
        Stat stat = curatorFramework.checkExists().forPath(dataSourcePath);
        String dataSourceContent = "jdbc:mysql://10.255.5.29:3306/iot?useUnicode=true&characterEncoding=utf-8&useSSL=false&serverTimezone=UTC,root,123456";
        if (stat == null) {
            curatorFramework.create().creatingParentsIfNeeded().withMode(CreateMode.PERSISTENT).forPath(dataSourcePath, dataSourceContent.getBytes());
        } else {
            curatorFramework.setData().forPath(dataSourcePath, dataSourceContent.getBytes());
        }
        return "OK";
    }
}
